<?php
/**
 * CONTENIDO extension pack (mpContenidoX), consists of multiple classes to
 * provide more features for Plugins/Modules.
 *
 * @package     mpContenidoX
 * @subpackage  PluginInstaller
 * @author      Murat Purc <murat@purc.de>
 * @copyright   Copyright (c) 2008-2012 Murat Purc (http://www.purc.de)
 * @license     http://www.gnu.org/licenses/gpl-2.0.html - GNU General Public License, version 2
 * @version     $Id: App.class.php 21 2012-07-16 21:33:14Z purcaholic $
 */


defined('CON_FRAMEWORK') or die('Illegal call');


/**
 * Plugin plugin installer application class. Initializes the installer.
 *
 * @package     mpContenidoX
 * @subpackage  PluginInstaller
 * @author      Murat Purc <murat@purc.de>
 * @copyright     2009 Murat Purc
 */
class mpPluginInstaller_App
{
    /**
     * Self instance
     * @var  mpPluginInstaller_App
     */
    private static $_instance;

    /**
     * Flag to store loaded status. Will be set to true in startup() function.
     * @var  bool
     */
    private static $_run = false;

    /**
     * CONTENIDO configuration array.
     * @var  array
     */
    private $_cfg;


    /**
     * Prevent public instantiation
     */
    protected function __construct()
    {
        global $cfg;

        if (!isset($cfg)) {
            $cfg = array();
        }
        $this->_cfg = & $cfg;
    }


    /**
     * Disable cloning
     */
    private function __clone()
    {
    }


    /**
     * Returns self instance
     *
     * @return  mpPluginInstaller_App
     */
    public static function getInstance()
    {
        if (self::$_instance == null) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }


    /**
     * Main function to call. Invokes all plugin installer application related functions.
     *
     * @return void
     */
    public function startup()
    {
        if (self::$_run) {
            return;
        }
        self::$_run = true;

        $this->_setupConstants();
        $this->_setupPrecheck();
        $this->_setupInitialization();
    }


    /**
     * Sets some constants
     *
     * @return void
     */
    protected function _setupConstants()
    {
        // there is nothing to do at the moment
    }


    /**
     * Does the initial security check
     *
     * @return void
     */
     protected function _setupPrecheck()
     {
        // include security class and check request variables
        include_once(CON_PATH . 'classes/class.security.php');
        Contenido_Security::checkRequests();
     }


    /**
     * Runs the Contenifo startup process, includes several files.
     *
     * @return void
     */
    protected function _setupInitialization()
    {
        global $cfg, $belang, $contenido, $sess, $auth, $perm;
        global $contenido_host, $contenido_database, $contenido_user, $contenido_password;

        include_once(CON_PATH . 'includes/startup.php');

        cInclude('includes', 'functions.general.php');

        $this->_cfg['debug']['backend_exectime']['fullstart'] = getmicrotime();

        cInclude('includes', 'functions.i18n.php');
        cInclude('includes', 'functions.api.php');
        cInclude('includes', 'functions.general.php');
        cInclude('includes', 'functions.database.php');
        cInclude('includes', 'functions.str.php');

        cInclude('classes', 'class.xml.php');
        cInclude('classes', 'class.navigation.php');
        cInclude('classes', 'class.template.php');
        cInclude('classes', 'class.backend.php');
        cInclude('classes', 'class.notification.php');
        cInclude('classes', 'class.area.php');
        cInclude('classes', 'class.action.php');
        cInclude('classes', 'class.layout.php');
        cInclude('classes', 'class.treeitem.php');
        cInclude('classes', 'class.user.php');
        cInclude('classes', 'class.group.php');
        cInclude('classes', 'class.cat.php');
        cInclude('classes', 'class.client.php');
        cInclude('classes', 'class.inuse.php');
        cInclude('classes', 'class.table.php');

        page_open(array(
            'sess' => 'Contenido_Session',
            'auth' => 'Contenido_Challenge_Crypt_Auth',
            'perm' => 'Contenido_Perm')
        );

        i18nInit($this->_cfg['path']['contenido'] . $this->_cfg['path']['locale'], $belang);
        cInclude('includes', 'cfg_language_de.inc.php');
    }


    /**
     * Main function to call to shutdown the app.
     *
     * @return void
     */
    public function shutdown()
    {
        if (!self::$_run) {
            return;
        }
        page_close();
    }

}
